﻿namespace EasyCMS.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using System.Configuration;
    using Pismire;

    public class Categorys : Common.ICategorys
    {
        #region Dal
        /// <summary>
        /// 通过依赖注入确定使用的数据源
        /// </summary>
        internal Dals.ICategory Dal
        {
            get
            {
                lock (syncDal)
                {
                    if (m_Dal == null)
                    {
                        m_Dal = EasyCMS.Common.Runtime.ObjectContainer.Resolve<Dals.ICategory>();
                    }
                }
                return m_Dal;
            }
        }
        private Dals.ICategory m_Dal;
        private object syncDal = new object();
        #endregion

        #region CategoryDataTable
        internal List<EasyCMS.Common.DataComponents.Category> CategoryDataTable
        {
            get
            {
                m_CategoryDataTable = (List<EasyCMS.Common.DataComponents.Category>)HeyCacher2.Caches.Get(CategoryDataTableCacheKey, HeyCacher2.Enums.SaveType.InProc) as List<EasyCMS.Common.DataComponents.Category>;
                if (m_CategoryDataTable == null)
                {
                    lock (syncCategoryDataTable)
                    {
                        m_CategoryDataTable = Dal.GetCategoryDataTable();
                        HeyCacher2.Caches.Set(CategoryDataTableCacheKey, m_CategoryDataTable, HeyCacher2.Enums.SaveType.InProc);
                    }
                }
                return m_CategoryDataTable;
            }
        }
        private List<EasyCMS.Common.DataComponents.Category> m_CategoryDataTable;
        private string CategoryDataTableCacheKey = "EasyCMS_CategoryDataTable";
        private object syncCategoryDataTable = new object();
        #endregion

        #region ICategorys 成员

        public EasyCMS.Common.DataComponents.Category GetByID(int id)
        {
            return CategoryDataTable.FirstOrDefault(c => c.ID == id);
        }

        public EasyCMS.Common.DataComponents.Category GetByCodeName(string codeName)
        {
            return CategoryDataTable.FirstOrDefault(c => c.CodeName == codeName);
        }

        public List<EasyCMS.Common.DataComponents.Category> GetList(EasyCMS.Common.Querys.Category query)
        {
            int recordCount;
            return GetList(query, out recordCount);
        }

        public List<EasyCMS.Common.DataComponents.Category> GetList(EasyCMS.Common.Querys.Category query, out int recordCount)
        {
            //初始化查询
            recordCount = 0;
            List<EasyCMS.Common.DataComponents.Category> results;
            var linquery = from c in CategoryDataTable select c;

            //执行查询条件
            if (query.ParentID > 0)
            {
                linquery = linquery.Where(c => c.ParentID == query.ParentID);
            }

            //执行排序条件

            //获取符合条件的数据总数
            if (query.ReadRecordCount) recordCount = linquery.Count();

            //获取符合条件的分页数据
            results = linquery.ToList();

            //数据分页
            results = results.Skip(query.FirstResult).Take(query.MaxResults).ToList();

            //返回结果
            return results;
        }

        public ExcuteState Add(ref EasyCMS.Common.DataComponents.Category category)
        {
            return Dal.Add(ref category);
        }

        public ExcuteState Update(ref EasyCMS.Common.DataComponents.Category category)
        {
            return Dal.Update(ref category);
        }

        public ExcuteState Delete(int id)
        {
            return Dal.Delete(id);
        }

        #endregion
    }
}
